package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.PartsManagement.EquipmentBasicInfo;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.ComponentItemClassificationDao;
import com.cn.mapper.dao.db5.PartsManagement.EquipmentBasicInfoDao;
import com.cn.mapper.dao.db5.PartsManagement.ExpenseItemInfoDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.PartsManagement.EquipmentBasicInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EquipmentBasicInfoServiceImpl implements EquipmentBasicInfoService {
    private final EquipmentBasicInfoDao equipmentBasicInfoDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public EquipmentBasicInfoServiceImpl(EquipmentBasicInfoDao equipmentBasicInfoDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {
        this.equipmentBasicInfoDao = equipmentBasicInfoDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
    }

    @Override
    public List<EquipmentBasicInfo> selectEquipmentBasicInfoAll() {
        return equipmentBasicInfoDao.selectEquipmentBasicInfoAll();
    }

    @Override
    public List<EquipmentBasicInfo> selectEquipmentBasicInfo(int limit, int page, String equipmentCode, String equipmentName) {
        return equipmentBasicInfoDao.selectEquipmentBasicInfo(limit, page, equipmentCode, equipmentName);
    }

    @Override
    public Result addEquipmentBasicInfo(int structureId, String equipmentName, int responsiblePerson, int custodian, int user, String specificationModel, String supplier, String description, String storageLocation, int equipmentStatus,  int equipmentType,  Integer procedures) {
        List<EquipmentBasicInfo> equipmentBasicInfos = equipmentBasicInfoDao.selectEquipmentBasicInfoAll();
        for (EquipmentBasicInfo eq : equipmentBasicInfos) {
            if (eq.getEquipmentName() == equipmentName) {
                return Result.failure("500", "设备名称重复,请重新输入");
            }
        }

        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentBasicInfoTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "EquipmentBasicInfoTable");

        //对照表
        Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentMechanicalStructureMappingTable");
        int presentValue1 = businessTypeTable1.getPresentValue();
        int stepLength1 = businessTypeTable1.getStepLength();
        int ID1 = presentValue1 + stepLength1;
        String code1 = String.valueOf(ID1);
        if (ID1 > businessTypeTable1.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "EquipmentMechanicalStructureMappingTable");
        int i = equipmentBasicInfoDao.addEquipmentBasicInfo(ID, code, equipmentName, responsiblePerson, custodian, user, specificationModel, supplier, description, storageLocation, equipmentStatus,equipmentType,procedures);
         equipmentBasicInfoDao.addEquipmentMechanicalStructureMapping(ID1,ID,structureId);
//        if (i == 0) {
//            return Result.failure("500", "提交失败");
//        }
        return Result.success("200", "提交成功");
    }

    @Override
    public Result deleteEquipmentBasicInfo(List<EquipmentBasicInfo> idList) {

        for (EquipmentBasicInfo eq : idList) {
            int i = equipmentBasicInfoDao.deleteEquipmentBasicInfo(eq.getID());
            int i1 = equipmentBasicInfoDao.deleteEquipmentMechanicalStructureMapping(eq.getID());
            if (i == 0) {
                return Result.failure("500", "删除失败");
            }
        }
        return Result.success("200", "删除成功");
    }

    @Override
    public Result upDateEquipmentBasicInfo(int structureId,int ID, String equipmentName, int responsiblePerson, int custodian, int user, String specificationModel, String supplier, String description, String storageLocation, int equipmentStatus ,int equipmentType,  Integer procedures) {
        List<EquipmentBasicInfo> equipmentBasicInfos = equipmentBasicInfoDao.selectEquipmentBasicInfoAll();
        for (EquipmentBasicInfo eq : equipmentBasicInfos) {
            if (eq.getEquipmentName() == equipmentName&&eq.getID()==ID) {
                return Result.failure("500", "设备名称重复,请重新输入");
            }
        }



        int i = equipmentBasicInfoDao.upDateEquipmentBasicInfo(ID, equipmentName, responsiblePerson, custodian, user, specificationModel, supplier, description, storageLocation, equipmentStatus,equipmentType, procedures);

        //对照表
        equipmentBasicInfoDao.deleteEquipmentMechanicalStructureMapping(ID);
        Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("EquipmentMechanicalStructureMappingTable");
        int presentValue1 = businessTypeTable1.getPresentValue();
        int stepLength1 = businessTypeTable1.getStepLength();
        int ID1 = presentValue1 + stepLength1;
        String code1 = String.valueOf(ID1);
        if (ID1 > businessTypeTable1.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "EquipmentMechanicalStructureMappingTable");
        equipmentBasicInfoDao.addEquipmentMechanicalStructureMapping(ID1,ID,structureId);



        if (i == 0) {
            return Result.failure("500", "修改失败");
        }
        return Result.success("200", "修改成功");
    }

    @Override
    public List<EquipmentBasicInfo> selectEquipmentBasicInfoAuto1(int limit, int page, String equipmentCode, String equipmentName) {
        return equipmentBasicInfoDao.selectEquipmentBasicInfoAuto1(limit,page,equipmentCode,equipmentName);
    }

}
